package GUILayer;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class MainMenuSimpleWithLogIn {
	private JFrame frame;
	private JPanel mainPanel;
	private JPanel mainContainer;
	private JPanel mainMenuPanel;
	private JPanel personPanel;
	private JPanel roomsPanel;
	private JPanel activityPanel;
	private JPanel salePanel;
	private JPanel servicePanel;
	private JPanel statisticsPanel;
	private JPanel agencyPanel;

	private JPanel discountPanel;
	private JPanel buttonPanel;
	private LoginUI loginUI;

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainMenuSimpleWithLogIn window = new MainMenuSimpleWithLogIn();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public MainMenuSimpleWithLogIn() {
		initialize();
	}

	private void initialize() {
		frame = new JFrame("Morroco Hotel System");
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				close();
			}
		});
		frame.setSize(1200, 740);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new CardLayout(0, 0));

		loginUI = new LoginUI();
		frame.getContentPane().add(loginUI, "loginUI");

		loginUI.getLoginBtnLogin().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loginUI.login();
				if (LoginUI.getLoggedInId() != 0) {
					loginInit();
				}
			}
		});

	}

	public void loginInit() {
		int authLevel = LoginUI.getLoggedInPosition();

		mainPanel = new JPanel();
		frame.getContentPane().add(mainPanel, "mainPanel");
		mainPanel.setLayout(new BorderLayout());
		mainMenuPanel = new JPanel();
		mainPanel.add(mainMenuPanel, BorderLayout.NORTH);
		mainMenuPanel.setLayout(new BorderLayout());

		buttonPanel = new JPanel();
		mainMenuPanel.add(buttonPanel, BorderLayout.WEST);

		mainContainer = new JPanel();
		mainContainer.setLayout(new CardLayout());
		mainPanel.add(mainContainer, BorderLayout.CENTER);

		JButton activityButton = new JButton("Activity");

		activityButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				disposeAll();
				activityPanel = new ActivityGUI();
				mainContainer.add(activityPanel, "activityPanel");
				CardLayout cl = (CardLayout) (mainContainer.getLayout());
				cl.show(mainContainer, "activityPanel");
				mainContainer.updateUI();
			}
		});
		buttonPanel.add(activityButton);
		activityButton.doClick();

		if (authLevel == 1 || authLevel == 2 || authLevel == 3) {
			JButton saleButton = new JButton("Sale");
			buttonPanel.add(saleButton);

			saleButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						frame.setCursor(Cursor
								.getPredefinedCursor(Cursor.WAIT_CURSOR));

						disposeAll();
						salePanel = new SaleGUI();
						mainContainer.add(salePanel, "salePanel");
						CardLayout cl = (CardLayout) (mainContainer.getLayout());
						cl.show(mainContainer, "salePanel");
						mainContainer.updateUI();
					} finally {
						frame.setCursor(Cursor.getDefaultCursor());
					}
				}
			});

			buttonPanel.add(activityButton);

			JButton roomButton = new JButton("Room");
			buttonPanel.add(roomButton);

			roomButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					disposeAll();
					try {
						frame.setCursor(Cursor
								.getPredefinedCursor(Cursor.WAIT_CURSOR));

						roomsPanel = new RoomTypeGUI();
						mainContainer.add(roomsPanel, "roomsPanel");
						CardLayout cl = (CardLayout) (mainContainer.getLayout());
						cl.show(mainContainer, "roomsPanel");
						mainContainer.updateUI();
					} finally {
						frame.setCursor(Cursor.getDefaultCursor());
					}

				}
			});

			JButton serviceButton = new JButton("Service");
			buttonPanel.add(serviceButton);

			serviceButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					disposeAll();
					servicePanel = new ServiceGUI();
					mainContainer.add(servicePanel, "servicePanel");
					CardLayout cl = (CardLayout) (mainContainer.getLayout());
					cl.show(mainContainer, "servicePanel");
					mainContainer.updateUI();
				}
			});

			JButton personButton = new JButton("Person");
			buttonPanel.add(personButton);

			personButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						frame.setCursor(Cursor
								.getPredefinedCursor(Cursor.WAIT_CURSOR));
						disposeAll();
						personPanel = new PersonGUI();
						mainContainer.add(personPanel, "personPanel");
						CardLayout cl = (CardLayout) (mainContainer.getLayout());
						cl.show(mainContainer, "personPanel");
						mainContainer.updateUI();
					} finally {
						frame.setCursor(Cursor.getDefaultCursor());
					}
				}
			});

			JButton agencyButton = new JButton("Agency");
			buttonPanel.add(agencyButton);

			agencyButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					disposeAll();
					agencyPanel = new AgencyGUI();
					mainContainer.add(agencyPanel, "agencyPanel");
					CardLayout cl = (CardLayout) (mainContainer.getLayout());
					cl.show(mainContainer, "agencyPanel");
					mainContainer.updateUI();
				}
			});
			saleButton.doClick();
		}

		if (authLevel == 1 || authLevel == 2) {
			JButton discountButton = new JButton("DiscountGroup");
			buttonPanel.add(discountButton);

			discountButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					disposeAll();
					discountPanel = new DiscountGUI();
					mainContainer.add(discountPanel, "discountPanel");
					CardLayout cl = (CardLayout) (mainContainer.getLayout());
					cl.show(mainContainer, "discountPanel");
					mainContainer.updateUI();
				}
			});

			JButton statisticsButton = new JButton("Statistics");
			buttonPanel.add(statisticsButton);

			statisticsButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					disposeAll();
					statisticsPanel = new StatisticsGUI();
					mainContainer.add(statisticsPanel, "statisticsPanel");
					CardLayout cl = (CardLayout) (mainContainer.getLayout());
					cl.show(mainContainer, "statisticsPanel");
					mainContainer.updateUI();
				}
			});
		}

		JLabel lbllogInd = new JLabel("Log In Name: "
				+ LoginUI.getLoggedInName());
		JButton logoutButton = new JButton("Log Out");

		JPanel logPanel = new JPanel();
		logPanel.setLayout(new FlowLayout());
		logPanel.add(lbllogInd);
		logPanel.add(logoutButton);
		mainMenuPanel.add(logPanel, BorderLayout.EAST);

		logoutButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.getContentPane().remove(mainPanel);
				LoginUI.logout();
				CardLayout cl = (CardLayout) frame.getContentPane().getLayout();
				cl.show(frame.getContentPane(), "loginUI");
			}
		});

		CardLayout cl = (CardLayout) frame.getContentPane().getLayout();
		cl.show(frame.getContentPane(), "mainPanel");
	}

	private void disposeAll() {
		try {
			for (Component comp : mainContainer.getComponents()) {
				System.out.println("MainMenuSimple - Removing component "
						+ comp.getClass().getName());
				mainContainer.remove(comp);
			}
		} catch (NullPointerException e) {

		}

	}

	public void close() {
		System.exit(1);
	}
}
